package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.WxCollege;
import com.wxxymakers.grademark.service.WxCollageService;
import com.wxxymakers.grademark.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR soft
 * @DATE 2018/12/27 17:57
 * @DESCRIBE
 */
@RestController
@RequestMapping("/collage")
public class CollegeController {

    @Autowired
    private WxCollageService collageService;

    @PostMapping("/add")
    public RestResponse add(WxCollege college) {
        if (collageService.add(college)) {
            return RestResponse.ok(college);
        }
        return RestResponse.fail();
    }

    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable int id) {
        if (collageService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/change")
    public RestResponse change(WxCollege college) {
        if (collageService.change(college)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/list")
    public RestResponse list(WxCollege college) {
        Page<WxCollege> colleges = collageService.page(1, Integer.MAX_VALUE, college);
        if (colleges != null) {
            return RestResponse.ok(colleges.getContent());
        }
        return RestResponse.fail();
    }

    @PostMapping("/page")
    public RestResponse page(@RequestParam(defaultValue = "1", required = false) int page,
                             @RequestParam(defaultValue = "10", required = false) int limit,
                             WxCollege college) {
        Page<WxCollege> colleges = collageService.page(page, limit, college);
        if (colleges != null) {
            return RestResponse.ok(colleges);
        }
        return RestResponse.fail();
    }
}
